package com.kth.subtranslator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.kth.subtranslator.driver.BabelFishDriver;
import com.kth.subtranslator.driver.GoogleTranslateDriver;
import com.kth.subtranslator.driver.WebTranslateApiDriver;

public class TranslateEngine {

	public static enum Language {
		
		FRENCH("French", "fr"),
		ENGLISH("English", "en"),
		SPANISH("Spanish", "es"),
		GERMAN("German", "de"),
		ITALIAN("Italian", "it"),
		DUTCH("Dutch", "nl"),
		RUSSIAN("Russian", "ru");
		
		private String name;
		private String localCode;

		private Language(String name, String localCode) {
			this.name = name;
			this.localCode = localCode;
		}
		
		public String getName() {
			return name;
		}
		
		public String getLocalCode() {
			return localCode;
		}
		
		@Override
		public String toString() {
			return getName();
		}
	}

	private List<ITranslateDriver> drivers = new ArrayList<ITranslateDriver>();
	private ITranslateDriver defaultDriver;
	private ITranslateDriver currentDriver;

	public TranslateEngine() {
		drivers.add( new GoogleTranslateDriver() );
		
		drivers.add( new BabelFishDriver() );
		drivers.add( new WebTranslateApiDriver() );
		
		setDefaultDriver(drivers.get(2));
	}

	public String translate(String text, Language fromLang, Language toLang) throws LanguageNotSupportedException {
		System.out.println("Using driver : " + getDefaultDriver().getName());
		checkSupportedLanguages(fromLang, toLang);
		return getDefaultDriver().translate(text, fromLang, toLang);
	}
	
	public Collection<String> batchTranslate(Collection<String> texts, Language fromLang, Language toLang) throws LanguageNotSupportedException {
		return batchTranslate(texts, fromLang, toLang, null);
	}
	
	public Collection<String> batchTranslate(Collection<String> texts, Language fromLang, Language toLang, IBatchTranslateCallBack callBack) throws LanguageNotSupportedException {
		return batchTranslate(getDefaultDriver(), texts, fromLang, toLang, callBack);
	}

	public Collection<String> batchTranslate(ITranslateDriver driver, Collection<String> texts, Language fromLang, Language toLang, IBatchTranslateCallBack callBack) throws LanguageNotSupportedException {
		currentDriver = driver;
		System.out.println("Using driver : " + driver.getName());
		checkSupportedLanguages(fromLang, toLang);
		return driver.batchTranslate(texts, fromLang, toLang, callBack);
	}
	
	private boolean isLanguageSupported(Language lang) {
		return getDefaultDriver().getSupportedLanguage().contains(lang);
	}

	private void checkSupportedLanguages(Language fromLang, Language toLang) throws LanguageNotSupportedException {
		if (!isLanguageSupported(fromLang)) {
			throw new LanguageNotSupportedException(fromLang);
		}
		else if (!isLanguageSupported(toLang)) {
			throw new LanguageNotSupportedException(toLang);
		}		
	}
	
	public Collection<Language> getSupportedLanguages() {
		return getDefaultDriver().getSupportedLanguage();
	}
	
	/**
	 * @return the drivers
	 */
	public ITranslateDriver getDefaultDriver() {
		return defaultDriver;
	}
	
	/**
	 * @return the drivers
	 */
	public List<ITranslateDriver> getDrivers() {
		return Collections.unmodifiableList( drivers );
	}
	
	/**
	 * @param driver the driver to set
	 */
	public void setDefaultDriver(ITranslateDriver driver) {
		defaultDriver = driver;
	}

	/**
	 * 
	 */
	public void stop() {
		if (currentDriver != null) {
			System.out.println("Stoping current driver : " + currentDriver.getName());
			currentDriver.stop();
		}
	}
	
}
